package com.ywq.app.web.mvc.artMade;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ywq.app.biz.ArtMadeBiz;
import com.ywq.app.biz.ArtPicBiz;
import com.ywq.app.biz.AuxContentBiz;
import com.ywq.app.domain.ArtArtist;
import com.ywq.app.domain.ArtArtistPic;
import com.ywq.app.domain.ArtClass;
import com.ywq.app.domain.ArtMade;
import com.ywq.app.domain.ArtRoom;
import com.ywq.app.domain.Content;
import com.ywq.app.domain.Member;
import com.ywq.app.domain.OnLineUserListener;
import com.ywq.app.web.mvc.artRoom.ArtRoomController;
import com.ywq.common.constant.Constant;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;
import com.ywq.common.util.Common;

@Controller
public class ArtMadeController {
	private final static Logger logger = LoggerFactory.getLogger(ArtRoomController.class);
	@Autowired
	private AuxContentBiz auxContentBiz;
	@Autowired
	private ArtPicBiz artPicBiz;
	@Autowired
	private ArtMadeBiz artMadeBiz;
	
	
	//定制首页
	@RequestMapping(value = "/made.htm", method = RequestMethod.GET)
	public String made(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			//轮播图
			List<Content> LbList = auxContentBiz.getRoomByChannel("madeTop", 6);
			//up
			List<Content> upList = auxContentBiz.getRoomByChannel("madeUp", 9);
			//左下文案
			List<Content> leftContent = auxContentBiz.getRoomByChannel("leftContent", 1);
			request.setAttribute("leftContent", leftContent.get(0));
			request.setAttribute("LbList", LbList);
			request.setAttribute("upList", upList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artMade/artMade";
		
	}
	//工作流程
	@RequestMapping(value = "/flow.htm", method = RequestMethod.GET)
	public String flow(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			List<Content> flowList = auxContentBiz.getRoomByChannel("madeFlow", 4);
			request.setAttribute("flowList", flowList);
			List<Content> madeFlowArt = auxContentBiz.getRoomByChannel("madeFlowArt", 1);
			request.setAttribute("madeFlowArt", madeFlowArt);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artMade/flow";
		
	}
	//艺术家列表页（作品列表页）
	@RequestMapping(value = "/martist.htm", method = RequestMethod.GET)
	public String martist(Integer page,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			//六分类（ 一级分类）
			List<ArtClass> classList = artPicBiz.getChildById("0",Constant.PIC_MADE_CLASS);
			request.setAttribute("classList", classList);
			//6个最新的艺术家   每个艺术家（作品4个）关联表查询最新4个 作品
			if (page == null) {
				page = 1;
			}
			PageModel modelMadeArtist = artMadeBiz.getMadeArtistCsList(page,null);
			request.setAttribute("modelMadeArtist", modelMadeArtist);
			request.setAttribute("classId", "a");
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artMade/martist";
		
	}
	//
	//推荐艺术家列表页（艺术家列表页 7个）
	@RequestMapping(value = "/niceist.htm", method = RequestMethod.GET)
	public String niceist(Integer page,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			if (page == null) {
				page = 1;
			}
			PageModel niceist = artMadeBiz.getMadeArtistList(page);
			request.setAttribute("niceist", niceist);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artMade/niceist";
		
	}
	//故事
	@RequestMapping(value = "/story.htm", method = RequestMethod.GET)
	public String story(String id,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			Content story =auxContentBiz.getStoryByArtistId(id);
			request.setAttribute("story", story);
			
			List<Content> storySix = auxContentBiz.getRoomByChannel("storySix", 6);
			
			request.setAttribute("storySix", storySix);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artMade/story";
		
	}
	//艺术家页面
	@RequestMapping(value = "/artist.htm", method = RequestMethod.GET)
	public String artist(String id,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			//根据艺术家ID查询作品
			List<ArtArtistPic> picList =artMadeBiz.getPicListByIstId(id);
			request.setAttribute("picList", picList);
			//故事
			Content story =auxContentBiz.getStoryByArtistId(id);
			request.setAttribute("story", story);
			//查询艺术及详情
			ArtArtist artist =artMadeBiz.getArtist(id);
			request.setAttribute("artist", artist);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artMade/artist";
		
	}
	//下定制页面
	@RequestMapping(value = "/madeArt.htm", method = RequestMethod.GET)
	public String madeArt(String id,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			ArtArtist artist =artMadeBiz.getArtist(id);
			request.setAttribute("artist", artist);
			OnLineUserListener onLineUserListener = (OnLineUserListener) request.getSession().getAttribute("onlineUser");
			if(onLineUserListener!=null){
				//查询艺术及详情
				Member member =onLineUserListener.getMember();
				request.setAttribute("member", member);
			}
			//madeArtRight
			List<Content> madeArtRight = auxContentBiz.getRoomByChannel("madeArtRight", 3);
			request.setAttribute("madeArtRight", madeArtRight);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artMade/madeArt";
		
	}
	//定制单子
	@RequestMapping(value = "/saveMade.htm", method = RequestMethod.POST)
	public String saveMade(ArtMade artMade,HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			OnLineUserListener onLineUserListener = (OnLineUserListener) request.getSession().getAttribute("onlineUser");
			if(!Common.empty(onLineUserListener.getMember())){
				//memberBiz.deleteMemberHas(hasId);
				artMade.setMemberId(onLineUserListener.getMember().getId());
				artMadeBiz.insterMade(artMade);
				pw = response.getWriter();
				pw.print("success");
			}else{
				pw = response.getWriter();
				pw.print("error");
			}
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("auction failure.", e);
			}
		}
		return null;
		}
	//分类
	@RequestMapping(value = "/martistcs.htm", method = RequestMethod.GET)
	public String martistcs(Integer page,String classId,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			//六分类（ 一级分类）
			List<ArtClass> classList = artPicBiz.getChildById("0",Constant.PIC_MADE_CLASS);
			request.setAttribute("classList", classList);
			//6个最新的艺术家   每个艺术家（作品4个）关联表查询最新4个 作品
			if (page == null) {
				page = 1;
			}
			PageModel modelMadeArtist = artMadeBiz.getMadeArtistCsList(page,classId);
			request.setAttribute("modelMadeArtist", modelMadeArtist);
			request.setAttribute("classId", classId);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artMade/martist";
		
	}
}
